// TrainingController.java
package com.sun.huazhi.aitaskservice.controller;

import com.sun.huazhi.aitaskservice.DTO.response.TrainingResponse;
import com.sun.huazhi.aitaskservice.common.config.PythonApiProperties;
import com.sun.huazhi.aitaskservice.common.resilience.ResilientServiceExecutor;
import io.swagger.v3.oas.annotations.Operation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.reactive.function.client.WebClient;
import reactor.core.publisher.Mono;
import io.swagger.v3.oas.annotations.tags.Tag;

@RestController
@RequestMapping("/api/training")
@Tag(name = "模型训练", description = "提供本地模型训练功能")
public class TrainingController {

    @Autowired
    @Qualifier("pythonApiProperties")
    private PythonApiProperties apiProps;

    @Autowired
    private WebClient webClient;

    @Autowired
    private ResilientServiceExecutor resilientExecutor;

    @PostMapping("/train-model")
    @Operation(summary = "训练本地模型",
            description = "启动本地模型的后台训练任务")
    public Mono<ResponseEntity<TrainingResponse>> trainLocalModel() {
        return resilientExecutor.execute(() ->
                webClient.post()
                        .uri(apiProps.getUrl() + "/train-local-model")
                        .retrieve()
                        .bodyToMono(TrainingResponse.class)
                        .map(ResponseEntity::ok)
        );
    }
}